package com.app.modules.home.view.mediators {
	import com.app.common.GlobalConstants;
	import com.app.modules.home.view.components.HomeView;
	import com.app.shell.message.ShellMessage;
	
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	public class HomeViewMediator {
		
		[MessageDispatcher] public var dispatcher:Function;
		
		[Bindable] 
		public var view:HomeView;
		
		[Observe]
		public function mediateView(view:HomeView):void {
			trace("Mediator.mediateView() called");
			
			this.view = view;
			
			if (view != null) {
				if (view.initialized) {
					viewInitialized();
				} else {
					view.addEventListener(FlexEvent.CREATION_COMPLETE, componentInitialized);
				}
			}
		}
		
		protected function componentInitialized(e:Event = null):void {
			trace("Mediator.componentInitialized(): View fully initialized, safe to interact");
			viewInitialized();
		}
		
		public function viewInitialized():void {
			trace("Mediator.init(): Initializing view");
			view.homeButton.addEventListener(MouseEvent.CLICK, reactToHomeButton$Click);
		}
		
		[Observe(phase="postDestroy")]
		public function viewDestroyed(view:HomeView):void {
			view.homeButton.removeEventListener(MouseEvent.CLICK, reactToHomeButton$Click);
		}		
		
		public function reactToHomeButton$Click(e:MouseEvent):void
		{
			(view.homeButton.label == "Home Button") ? view.homeButton.label = "Home clicked. Click Again" : view.homeButton.label = "Home Button";
			dispatcher(new ShellMessage(GlobalConstants.LOG_MESSAGE, 'Label Changed: ' +view.homeButton.label));
		}
		
	}
}